import re
from rest_framework import serializers

from .models import Projects, SystemDictData, Apis, ApiCase


class ProjectsSerializer(serializers.Serializer):

	id = serializers.IntegerField(read_only=True)
	name = serializers.CharField(label='项目名称', required=True)
	mark = serializers.CharField(label='项目代号', required=True)
	protocol = serializers.IntegerField(label="协议", required=True)
	host = serializers.URLField(label='主机地址', required=True)
	desc = serializers.CharField(label='描述信息', required=False)
	status = serializers.BooleanField(read_only=True)

	class Meta:
		model = Projects

	# def validate_host(self, value):
	# 	if not re.match(r'(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?', value):
	# 		raise serializers.ValidationError('URL 格式不正确')
	# 	return value
	#
	# def create(self, validated_data):
	# 	project = Projects.objects.create(**validated_data)
	# 	return project


class SystemDictDataSerializer(serializers.Serializer):

	id = serializers.IntegerField(read_only=True)
	dict_name = serializers.CharField(label='字典名', required=True)
	dict_value = serializers.CharField(label="字典值", required=True)
	dict_label = serializers.CharField(label='字典类型', required=True)
	desc = serializers.CharField(label='描述信息', required=False)
	status = serializers.BooleanField(read_only=True)

	class Meta:
		model = SystemDictData

	def create(self, validated_data):
		sys_dict = SystemDictData.objects.create(**validated_data)
		return sys_dict


class ApisSerializer(serializers.Serializer):

	id = serializers.IntegerField(read_only=True)
	name = serializers.CharField(label='接口名', required=True)
	project_id = serializers.IntegerField(label='项目ID', required=True)
	project_mark = serializers.CharField(label='项目代号', source='project.mark', read_only=True)
	url = serializers.CharField(label='接口', required=True)
	status = serializers.BooleanField(label='任务状态', read_only=True)
	desc = serializers.CharField(label='描述信息')

	class Meta:
		model = Apis


class ApiCaseSerializer(serializers.Serializer):
	id = serializers.IntegerField(read_only=True)
	case_name = serializers.CharField(label='用例名', required=True)
	api_id = serializers.IntegerField(label='接口ID', required=True)
	request_type_id = serializers.IntegerField(label='请求方式', required=True)
	request_type = serializers.CharField(label='请求方式', source='request_type.dict_value')
	headers = serializers.CharField(label='请求头', required=True)
	param_type_id = serializers.IntegerField(label='参数类型', required=True)
	param_type = serializers.CharField(label='参数类型', source='param_type.dict_value')
	param = serializers.CharField(label='参数', required=True)
	job_type_id = serializers.IntegerField(label='任务类型', required=True)
	job_type = serializers.CharField(label='任务类型', source='job_type.dict_value')
	job_cron = serializers.CharField(label='cron表达式', required=True)
	desc = serializers.CharField(label='描述信息')
	status = serializers.IntegerField(label='任务状态', read_only=True)
	task_id = serializers.CharField(label='调度ID', read_only=True)
	expect_type_id = serializers.IntegerField(label='预期类型', required=True)
	expect_type = serializers.CharField(label='预期类型', source='expect_type.dict_value')
	expect = serializers.CharField(label='预期', required=True)

	class Meta:
		model = ApiCase
